package net.mmgg.android.bm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ProgressDialog progressDialog = null; 
	private static final int MESSAGESTATUS = 0x0001;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		long now = new Date().getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dates = sdf.parse("2014-07-20");
			if (now > dates.getTime()) {
				System.exit(0);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Config cfg = (Config) getApplication();
		Button main_ok = (Button) findViewById(R.id.main_ok);
		final EditText main_num = (EditText) findViewById(R.id.main_num);
		main_num.setText("1");
		if (null == cfg.getCode()) {
			Intent initIntent = new Intent(MainActivity.this, InitActivity.class);
			startActivity(initIntent);
			finish();
		} else {
			if (!NetWorkUtil.isNetworkAvailable(this)) {
				new AlertDialog.Builder(this).setTitle("温馨提示").setMessage("请检查网络连接!").setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialoginterface, int i) {
						finish();
					}
				}).show();

				return;
			}else{
				//如果网络已经连接,则进行IMEI号验证
				TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);     
				final String imei=tm.getDeviceId();
				//String imeiUrl="http://bd.xxooooo.com/?id=shenzhen&im=358239054065232";
				progressDialog = ProgressDialog.show(MainActivity.this, "请稍后", "正在初始化,请稍候！");       
				new Thread(new Runnable() {
					@Override
					public void run() {
						String imeiUrl="http://xadrdq.com/imei/?im="+imei;
						String result=CustomerHttpClient.get(imeiUrl);
						if(result!=null&&result.trim().equalsIgnoreCase("valid")){
							
						}else{
							progressDialog.dismiss(); // 关闭进度条
							finish();
							return;
						}
						
						String ip=CustomerHttpClient.get("http://ip.3322.org");
						ip=ip.trim();
						NetWorkUtil.ipAddress=ip;
						Message msg_listData = new Message();
                        msg_listData.what = MESSAGESTATUS;
                        handler.sendMessage(msg_listData);
						
					}
				}).start();
				
			}
			main_ok.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String numStr = main_num.getText().toString();
					if ("".equals(numStr)) {
						Toast.makeText(getApplicationContext(), "请正确填写执行次数!", Toast.LENGTH_LONG).show();
						return;
					}
					Intent in = new Intent(MainActivity.this, TabsActivity.class);
					in.putExtra("num", Integer.parseInt(numStr));
					startActivity(in);
				}
			});
		}

	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message message) {
			switch (message.what) {
			case MESSAGESTATUS:
				// 刷新UI，显示数据，并关闭进度条
				progressDialog.dismiss(); // 关闭进度条
				break;
			}
		}
	};
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
//		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			Toast.makeText(getApplicationContext(), "作者:吴云辉\n\nQQ:963415\n\nEmail:Fly@mmgg.net", Toast.LENGTH_LONG).show();
//			return true;
//		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		finish();
	}

}
